﻿Imports EnvDTE
Imports EnvDTE80
Imports System.IO
Public Class SourceCode_worker

#Region "private attributes"
    Private aPathTemplate As String  ' cesta k template solution
    Private aPathDestination As String
    Private aProjectName As String
    Private aFileNames As Files
#End Region

#Region "properties"

#End Region

#Region "constructors"
    ''' <summary>
    ''' konstruktor
    ''' </summary>
    ''' <param name="pPathTemplate">cesta k rood adresaru s templatmi</param>
    ''' <param name="pPathDestination">cielova cesta kde sa vytvori projekt</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal pFileNames As Files, ByVal pPathTemplate As String, ByVal pPathDestination As String, ByVal pProjectName As String)
        Me.aFileNames = pFileNames
        Me.aPathDestination = pPathDestination
        Me.aPathTemplate = pPathTemplate
        Me.aProjectName = pProjectName
    End Sub

    Public Sub New()
    End Sub
#End Region

#Region "private methods"
    ''' <summary>
    ''' skopiruje template solution na cielove miesto z ktoreho sa vytvori projekt
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub copyTemplateSolutionToDestination()
        My.Computer.FileSystem.CreateDirectory(Path.Combine(aPathDestination, aProjectName))
        My.Computer.FileSystem.CopyDirectory(Path.Combine(aPathTemplate, Me.aFileNames.mFolderSolutionTemplates), Path.Combine(aPathDestination, aProjectName), True)
    End Sub

    ''' <summary>
    ''' Spravi string z templatoveho suboru
    ''' </summary>
    ''' <param name="pPath">Cesta k textovemu suboru na disku</param>
    ''' <returns></returns>
    Private Function readTemplateIntoString(ByVal pPath As String) As String   'to iste co zuzka citaj template do stringu
        Dim lRetVal As String
        Dim fso, inputFile

        fso = CreateObject("Scripting.FileSystemObject")
        Try
            inputFile = fso.OpenTextFile(pPath, 1)
            lRetVal = inputFile.ReadAll
            inputFile.Close()
        Catch ex As Exception
            Throw New Exception("CHYBA - Template pre vytvorenie projektu sa nenašiel. " & vbCrLf & ex.Message, ex)
        End Try
        Return lRetVal
    End Function

    ''' <summary>
    ''' Zapise string do suboru
    ''' </summary>
    ''' <param name="pPath">Cesta, kam sa zapise subor</param>
    ''' <param name="pSourceCode">Zdrojovy kod suboru</param>
    Public Sub WriteFileOnDisk(ByVal pPath As String, ByVal pSourceCode As String)
        Dim outputFile, fso
        fso = CreateObject("Scripting.FileSystemObject")
        Try
            outputFile = fso.CreateTextFile(pPath, True)
            outputFile.Write(pSourceCode)
            outputFile.Close()
        Catch ex As Exception
            Throw New Exception("CHYBA - Súbor sa nedá uložiť. " & vbCrLf & ex.Message, ex)
        End Try
    End Sub



#End Region

#Region "public methods"
    ''' <summary>
    ''' Vytvori solution s holou kostrou projektu
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub createSolution()
        Dim lPathDestination As String = aPathDestination

        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.mFolderSolutionTemplates & "\" & Me.aFileNames.mFileTplSolution, lPathDestination & "\" & Me.aProjectName & Me.aFileNames.mExtensionSln, True)

        'lPathDestination = lPathDestination & "\" & Me.aFileNames.mFolderBaseProject

        ' skopirovanie adresarov z template projektu na novy projekt
        My.Computer.FileSystem.CopyDirectory(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderBinTemplate, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFolderBin, True)
        My.Computer.FileSystem.CopyDirectory(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderEnumsTemplate, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFolderEnums, True)
        My.Computer.FileSystem.CopyDirectory(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderMyProjectTemplate, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFolderMyProject, True)
        My.Computer.FileSystem.CopyDirectory(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderObjTemplate, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFolderObj, True)

        ' skopirovanie suborov z template projektu na novy projekt
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplInit, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplInit, True)
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplFrmMain, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplFrmMain, True)
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplFrmMainDesigner, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplFrmMainDesigner, True)
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplFrmResx, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplFrmResx, True)
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplProjectFile, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplProjectFile, True)
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplProjectFileUser, lPathDestination & "\" & Me.aProjectName & "\" & Me.aFileNames.mFileTplProjectFileUser, True)

    End Sub

    Public Sub cteateProject(ByVal pModul As String, ByVal pClass As String)
        Dim aPathTmp As String = Path.Combine(Me.aPathDestination, Me.aFileNames.mPrefixFrm & pModul & "_" & pClass)
        My.Computer.FileSystem.CreateDirectory(aPathTmp)
        My.Computer.FileSystem.CreateDirectory(Path.Combine(aPathTmp, Me.aFileNames.mFolderBin))
        My.Computer.FileSystem.CreateDirectory(Path.Combine(aPathTmp, Me.aFileNames.mFolderObj))
        My.Computer.FileSystem.CopyFile(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderProjectTemplate & "\" & Me.aFileNames.mFileTplProjectOneFrm, aPathTmp & "\" & Me.aFileNames.mPrefixFrm & pModul & "_" & pClass & aFileNames.mExtensionVbproj, True)

        My.Computer.FileSystem.CopyDirectory(Me.aPathTemplate & "\" & Me.aFileNames.CompleteFolderMyProjectTemplate, aPathTmp & "\" & Me.aFileNames.mFolderMyProject, True)

    End Sub



    ''' <summary>
    ''' prida mod , dat , log subory do konkretneho modul adresara
    ''' </summary>
    ''' <param name="pPathFileTemplates">cesta k templejtom mod,dat,log suboru</param>
    ''' <param name="pPathDestination">cieľová cesta kam sa uložia súbory</param>
    ''' <param name="pUnits">názov unitov</param>
    ''' <param name="pModul">názov modulu</param>
    ''' <remarks></remarks>
    ''' 

    Public Sub addFiles(ByVal pPathFileTemplates As String, ByVal pPathDestination As String, ByVal pUnits As List(Of String), ByVal pUnitRegVw As String, ByVal pUnitRegFrm As String, ByVal pModul As String)
        Dim lUnitsPart
        ' log file pre kazdy formular
        ' Me.mFrmGen = New GeneratorFormularov(Path.Combine(Me.mPathRootTemplate, Me.mFileNames.mPathFormTemplates), lDat)
        For Each lUnitName As String In pUnits

            Dim aLogFile As String = Me.readTemplateIntoString(Path.Combine(pPathFileTemplates, Me.aFileNames.mFileTplLogTemplate))
            Dim aLogFileName As String = Me.aFileNames.mPrefixLog & lUnitName & Me.aFileNames.mExtensionVBSource
            aLogFile = Replace(aLogFile, "#log_UnitName#", Me.aFileNames.mPrefixLog & lUnitName)
            Me.WriteFileOnDisk(Path.Combine(pPathDestination, aLogFileName), aLogFile)

        Next

        ' dat file
        Dim aDatFile As String = Me.readTemplateIntoString(Path.Combine(pPathFileTemplates, Me.aFileNames.mFileTplDatTemplate))
        Dim aDatFileName As String = Me.aFileNames.mPrefixDat & pModul & Me.aFileNames.mExtensionVBSource
        aDatFile = Replace(aDatFile, "#dat_ModulName#", Me.aFileNames.mPrefixDat & pModul)
        aDatFile = Replace(aDatFile, "#mod_ModulName_xmod#", Me.aFileNames.mPrefixMod & pModul & Me.aFileNames.mExtensionXmod)
        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aDatFileName), aDatFile)

        ' mod file 
        ' dano dorobit.......
        Dim aModFile As String = Me.readTemplateIntoString(Path.Combine(pPathFileTemplates, Me.aFileNames.mFileTplModTemplate))
        Dim aModFileName As String = Me.aFileNames.mPrefixMod & pModul & Me.aFileNames.mExtensionVBSource
        aModFile = Replace(aModFile, "#mod_ModulName#", Me.aFileNames.mPrefixMod & pModul)
        aModFile = Replace(aModFile, "#ModulName#", pModul)
        aModFile = Replace(aModFile, "#dat_ModulName#", Me.aFileNames.mPrefixDat & pModul)
        ' zuzka  

        ' vygenerujem formular do modulu ktory bude zoznamovy (hlavny)

        ' vygenerujem logiku k tomuto hlavnemu fomrularu
        Dim aLogFileMainFormular As String = Me.readTemplateIntoString(Path.Combine(pPathFileTemplates, Me.aFileNames.mFileTplLogTemplate))
        Dim aLogFileNameMainFormular As String = Me.aFileNames.mPrefixLog & pModul & Me.aFileNames.mExtensionVBSource
        aLogFileMainFormular = Replace(aLogFileMainFormular, "#log_UnitName#", Me.aFileNames.mPrefixLog & pModul)
        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aLogFileNameMainFormular), aLogFileMainFormular)

        ' zuzka end



        'registracia List_view modulu a 
        'registracia unitov
        lUnitsPart = pUnitRegVw
        lUnitsPart = lUnitsPart & vbCrLf & pUnitRegFrm

        aModFile = Replace(aModFile, "'#UnitsReg#", "'#UnitsReg#" & vbCrLf & lUnitsPart)


        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aModFileName), aModFile)


    End Sub
    Public Sub updateModFile(ByVal pModul As String, ByVal pRegUnitFrm As String)
        Dim pathModDest As String = Me.aPathDestination + "\" + Me.aProjectName + "\" + aFileNames.mFolderModules + "\" + pModul + "\" + aFileNames.mPrefixMod + pModul + aFileNames.mExtensionVBSource
        Dim aModFile As String = Me.readTemplateIntoString(pathModDest)
        aModFile = Replace(aModFile, "'#UnitsReg#", "'#UnitsReg#" & vbCrLf & pRegUnitFrm)


        Me.WriteFileOnDisk(pathModDest, aModFile)
    End Sub


    ''' <summary>
    ''' upravi a doplni INIT.VB subor
    ''' </summary>
    ''' <param name="pPathDestination">Cesta kde sa ulozi vratane nazvu suboru init.vb</param>
    ''' <param name="pDbPath">cesta k fyzickej databazy</param>
    ''' <param name="pDbUser">user Name</param>
    ''' <param name="pDbPassword">password</param>
    ''' <param name="pModules">moduly ktore treba zaregistrovat v Init.vb</param>
    ''' <remarks></remarks>
    Public Sub updateInitFile(ByVal pPathDestination As String, ByVal pServerName As String, ByVal pDbPath As String, ByVal pDbUser As String, ByVal pDbPassword As String, ByVal pModules As String())
        Dim aInitFile As String = Me.readTemplateIntoString(Path.Combine(pPathDestination, Me.aFileNames.mFileTplInit))
        Dim aInitFileName As String = Me.aFileNames.mFileTplInit
        Dim lInitPart As String = ""
        aInitFile = Replace(aInitFile, "#server_name#", pServerName)
        aInitFile = Replace(aInitFile, "#db_path#", pDbPath)
        aInitFile = Replace(aInitFile, "#db_userName#", pDbUser)
        aInitFile = Replace(aInitFile, "#db_password#", pDbPassword)
        If (pModules.Count = 0) Then
            lInitPart = ""
        Else

            For i As Integer = 0 To pModules.Count - 1
                lInitPart += "xLib.ModuleList.AddModul(New " & Me.aFileNames.mPrefixMod & pModules(i) & "())" & vbCrLf
            Next
        End If
        aInitFile = Replace(aInitFile, "#init_modules#", lInitPart)
        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aInitFileName), aInitFile)
    End Sub

    ''' <summary>
    ''' metoda ktora prida referencie na pridane subory do projektoveho suboru .vbvroj
    ''' </summary>
    ''' <param name="pPathProjectFile">cesta aj s menom projektoveho suboru .vbproj</param>
    ''' <param name="pFilesForAddReference">list suborov s relativnymi cestami, ktore boli pridane do noveho projektu, </param>
    ''' <remarks></remarks>
    Public Sub addReferenciesIntoProjectFile(ByVal pPathProjectFile As String, ByVal pFilesForAddReference As List(Of String))
        Dim aProjectFile As String = Me.readTemplateIntoString(Path.Combine(pPathProjectFile, Me.aFileNames.mFileTplProjectFile))
        Dim aProjectFileName As String = Me.aFileNames.mFileTplInit
        Dim lReferecePart As String = ""
        If (pFilesForAddReference.Count = 0) Then
            lReferecePart = ""
        Else

            For Each lfileForAddReference As String In pFilesForAddReference
                lReferecePart += "<Compile Include=" & lfileForAddReference & " />" & vbCrLf
            Next
        End If
        aProjectFile = Replace(aProjectFile, "#reference_ToAddedSourceCodeFiles#", lReferecePart)
        Me.WriteFileOnDisk(Path.Combine(pPathProjectFile, aProjectFileName), aProjectFile)
    End Sub

    ''' <summary>
    ''' prida referencie do projektoveho suboru .vbproj na vsetky subory ktore boli pridane pri generovani
    ''' </summary>
    ''' <param name="pModul"></param>
    ''' <param name="pPathProjectFile"></param>
    ''' <param name="pFilesForAddReference"></param>
    ''' <remarks></remarks>
    Public Sub addFormsReferenciesIntoProjectFile(ByVal pModul As String, ByVal pPathProjectFile As String, ByVal pFilesForAddReference As List(Of String))
        Dim aProjectFile As String = Me.readTemplateIntoString(Path.Combine(pPathProjectFile, Me.aFileNames.mFileTplProjectFile))
        Dim aProjectFileName As String = Me.aFileNames.mFileTplInit
        Dim lReferecePart As String = ""
        If (pFilesForAddReference.Count = 0) Then
            lReferecePart = ""
        Else

            '        For Each lfileForAddReference As String In pFilesForAddReference
            '            lReferecePart += "<Compile Include=" & mFileNames.mFolderModules &Modules\Modul\frm_Formular.Designer.vb">
            '                                 <DependentUpon>frm_Formular.vb</DependentUpon>
            '                             </Compile>

            '<Compile Include="Modules\Modul\frm_Formular.vb">
            '  <SubType>Form</SubType>
            '</Compile> & vbCrLf
            '        Next
        End If
        aProjectFile = Replace(aProjectFile, "#reference_ToAddedFormulars#", lReferecePart)
        Me.WriteFileOnDisk(Path.Combine(pPathProjectFile, aProjectFileName), aProjectFile)
    End Sub

    ''' <summary>
    ''' upravi Solution file, prida referenciu na KrosLib
    ''' </summary>
    ''' <param name="pPathDestination">cesta noveho projektu</param>
    ''' <param name="pPathKrosLibProjectFile">cesta k projektu KrosLib</param>
    ''' <remarks></remarks>
    Public Sub updateSolutionFile(ByVal pPathDestination As String, ByVal pPathKrosLibProjectFile As String, ByVal pPathNovyProjectFile As String)
        Dim aSolutionFile As String = Me.readTemplateIntoString(Path.Combine(pPathDestination, Me.aProjectName & Me.aFileNames.mExtensionSln))
        Dim aSolutionFileName As String = Me.aProjectName & Me.aFileNames.mExtensionSln
        Dim lInitPart As String = ""
        aSolutionFile = Replace(aSolutionFile, "#KrosLibPath_ProjectFile#", pPathKrosLibProjectFile)
        aSolutionFile = Replace(aSolutionFile, "#NovyProject_ProjectFile#", pPathNovyProjectFile)

        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aSolutionFileName), aSolutionFile)
    End Sub

    Public Sub updateProjectFile(ByVal pPathDestination As String, ByVal pOtherFiles As List(Of String), ByVal pResxFiles As Dictionary(Of String, String), ByVal pVBFiles As List(Of String))
        Dim aProjectFile As String = Me.readTemplateIntoString(Path.Combine(pPathDestination, Me.aFileNames.mFileTplProjectFile))
        Dim aProjectFileName As String = Me.aFileNames.mFileTplProjectFile
        Dim aFilesReferencesPart As String = ""


        ' amod a xmod files
        For Each aFile As String In pOtherFiles
            aFilesReferencesPart += "<None Include=""" & aFile & """/>" & vbCrLf
        Next
        aProjectFile = Replace(aProjectFile, "<!--supportFiles-->", "<!--supportFiles-->" & vbCrLf & aFilesReferencesPart)

        aFilesReferencesPart = ""


        ' resx files
        For Each aFile As KeyValuePair(Of String, String) In pResxFiles
            aFilesReferencesPart += "<EmbeddedResource Include=""" & aFile.Value & """>" & vbCrLf _
            & "<DependentUpon>" & aFile.Key & "</DependentUpon>" & vbCrLf _
            & "</EmbeddedResource>" & vbCrLf
        Next
        aProjectFile = Replace(aProjectFile, "<!--resxFiles-->", "<!--resxFiles-->" & vbCrLf & aFilesReferencesPart)

        aFilesReferencesPart = ""


        ' vb files
        For Each aFile As KeyValuePair(Of String, String) In pResxFiles
            aFilesReferencesPart += "<Compile Include=""" & aFile.Value.Replace(aFileNames.mExtensionResx, aFileNames.mExtensionDesigner) & """>" & vbCrLf _
            & "<DependentUpon>" & aFile.Key & "</DependentUpon>" & vbCrLf _
            & "</Compile>" & vbCrLf
        Next

        For Each aFile As String In pVBFiles

            aFilesReferencesPart += "<Compile Include=""" & aFile & """>" & vbCrLf _
            & "<SubType>Code</SubType>" & vbCrLf _
            & "</Compile>" & vbCrLf
        Next
        aProjectFile = Replace(aProjectFile, "<!--vbFiles-->", "<!--vbFiles-->" & vbCrLf & aFilesReferencesPart)



        Me.WriteFileOnDisk(Path.Combine(pPathDestination, aProjectFileName), aProjectFile)

    End Sub

#Region "temporary"
    Public Sub updateMainForm(ByVal pPathFileDestinationVB As String, ByVal pPathFileDestinationDesigner As String, ByVal pCompInit As String, ByVal pCompSet As String, ByVal pCompAdd As String, ByVal pCompFriend As String, ByVal pCompCode As String)

        Dim aFile As String = Me.readTemplateIntoString(pPathFileDestinationDesigner)

        aFile = Replace(aFile, "'#components_Init#", "'#components_Init#" & vbCrLf & pCompInit)
        aFile = Replace(aFile, "'#components_set#", "'#components_set#" & vbCrLf & pCompSet)
        aFile = Replace(aFile, "'#components_Add#", "'#components_Add#" & vbCrLf & pCompAdd)
        aFile = Replace(aFile, "'#components_Friend#", "'#components_Friend#" & vbCrLf & pCompFriend)
        Me.WriteFileOnDisk(pPathFileDestinationDesigner, aFile)

        Dim aFileVB As String = Me.readTemplateIntoString(pPathFileDestinationVB)

        aFileVB = Replace(aFileVB, "'#components_code#", "'#components_code#" & vbCrLf & pCompCode)
        Me.WriteFileOnDisk(pPathFileDestinationVB, aFileVB)
    End Sub
#End Region
#End Region

End Class
